package com.linux4fun.singleton;

/**
 * 线程安全的单例类（方法级别的同步锁，存在性能问题）
 * https://kylepeng93.github.io
 *
 * @author pengkai
 * @date 2021-03-21 上午11:50
 */
public class ThreadSafeSingleton {
    private static ThreadSafeSingleton instance;

    private ThreadSafeSingleton() {}

    /**
     * 使用synchronized关键字保证线程安全
     * @return
     */
    public static synchronized ThreadSafeSingleton getInstance() {
        if (instance == null) {
            instance = new ThreadSafeSingleton();
        }
        return instance;
    }

}
